<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：已接任务控制器。
 *
 **/
namespace Api\Controller;

class UsertaskController extends ComController
{   
    
    /**
     * 已接任务分页列表
     */
    public function index()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : 1;
        $pagesize = intval(isset($_GET['pagesize']) ? intval($_GET['pagesize']) : '20');
        $status = I('get.status','','htmlentities');
        if(!$status){
            $this->res['code'] = 101;
            $this->res['msg'] = '请传入状态值status';
            $this->response($this->res,'json');
        }
        
        $model = M();
        $sqlWhere = "1=1 and ";
        if($status != ''){
           if($status == -1){
               $status = 0;
           }
           if($status == 2){
               $sqlWhere .= "a.`status` in (2,4) and ";
           }else{
               $sqlWhere .= "a.`status`=".$status." and ";
           }
        }
        if($this->user['id']){
            $sqlWhere .= "a.uid=".$this->user['id']." and ";
        }
        $sqlWhere = msubstr($sqlWhere, 0, utf8_strlen($sqlWhere) - 4, "utf-8", false);
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $model->query("select count(*) as total from ".C('DB_PREFIX')."user_task a left join ".C('DB_PREFIX')."task b on a.task_id=b.id where ".$sqlWhere);
        $count = $count[0]['total'];
        $sql = "select a.id,a.price,b.`title`,a.sh_t,a.`status`,b.cid,b.end_t,b.url,b.pay_status,b.user_level,b.money_type,b.uid from ".C('DB_PREFIX')."user_task a left join ".C('DB_PREFIX')."task b on a.task_id=b.id where ".$sqlWhere." order by a.t desc limit ".$offset . "," . $pagesize;
        $list = $model->query($sql);
        foreach ($list as $k=>&$v){
            // 获取平台名称及icon
            $category = M('category')->where(array('id'=>$v['cid']))->find();
            $list[$k]['icon'] = C('URL').$category['icon'];
            $list[$k]['cate_name'] = $category['title'];
            // 获取任务满足条件会员等级
            $user_level = $v['user_level'];
            $info = M('user_level')->where(array('id'=>$user_level))->field('title,bg_color')->find();
            $list[$k]['level_info'] = $info['title'];
            $list[$k]['bg_color'] = $info['bg_color'];
            // 任务要求
            $money_type = $v['money_type'];
            $money_type_arr = explode(',', $money_type);
            $money_info_title = '';
            foreach ($money_type_arr as $h=>&$l){
                $money_info = M('money_type')->where(array('id'=>$l))->field('title')->find();
                $money_info_title .= $money_info['title'].' ';
            }
            $list[$k]['money_info'] = $money_info_title;
            // 发布人
            if($v['uid'] == 0){
                $list[$k]['author'] = '平台';
            }else{
                $user = M('user')->where(array('id'=>$v['uid']))->find();
                $list[$k]['author'] = $user['nickname'];
            }
            if($v['sh_t']){
                $list[$k]['finish_t'] = date('Y-m-d',$v['sh_t']);
            }
			
			//任务审核备注
			$id = $v['id'];
			$task_info = M('user_task')->where('id='.$id)->find();
			$list[$k]['sh_remark'] =$task_info['sh_remark'];
			//任务task_id
			$list[$k]['task_id'] = $task_info['task_id'];
        }
        $page = new \Think\Page($count, $pagesize);
        $page->show();
        $page_arr = array(
            'totalCount'=>intval($count),
            'pageSize'=>$pagesize,
            'totalPage'=>$page->totalPages,
            'currPage'=>intval($p)
        );
        $this->res['data']['page'] = $page_arr;
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
    
    
    /**
     * 查看任务接单详情
     */
    public function taskDetail()
    {
        $id = I('get.id','','htmlentities');
        if(!$id){
            $this->res['code'] = 101;
            $this->res['msg'] = '请传入接单id';
            $this->response($this->res,'json');
        }
        $flag = M('user_task')->where('id='.$id)->count();
        if($flag == 0){
            $this->res['code'] = 102;
            $this->res['msg'] = '记录为空';
            $this->response($this->res,'json');
        }
        $sql = "select a.*,b.`title`,b.cid,b.end_t,b.url,b.pay_status,b.user_level,b.money_type,b.uid as user_id,c.username from ".C('DB_PREFIX')."user_task a left join ".C('DB_PREFIX')."task b on a.task_id=b.id left join ".C('DB_PREFIX')."user c on a.uid=c.id where a.id='{$id}'";
        $model = M();
        $query = $model->query($sql);
        $vo = $query[0];
        $pics = array();
        if($vo['pics'] != ''){
            $pics = explode(',', $vo['pics']);
            foreach ($pics as $key=>&$value){
                $pics[$key] = C('URL').$value;
            }
        }
        // 获取平台名称及icon
        $category = M('category')->where('id='.$vo['cid'])->find();
        $vo['icon'] = C('URL').$category['icon'];
        $vo['cate_name'] = $category['title'];
        // 获取任务满足条件会员等级
        $user_level = $vo['user_level'];
        $user_level_arr = explode(',', $user_level);
        $where1['id'] = array('in',$user_level_arr);
        $info = M('user_level')->where($where1)->field('title')->select();
        foreach ($info as $val) {
            $val = join(",",$val);
            $temp_array[] = $val;
        }
        $vo['level_info'] = implode(",", $temp_array);
        // 任务要求
        $money_type = $vo['money_type'];
        $money_type_arr = explode(',', $money_type);
        $where2['id'] = array('in',$money_type_arr);
        $money_info = M('money_type')->where($where2)->field('title')->select();
        foreach ($money_info as $val) {
            $val = join(",",$val);
            $temp_array1[] = $val;
        }
        $vo['money_info'] = implode(",", $temp_array1);
        // 发布人
        if($vo['uid'] == 0){
            $vo['author'] = '平台';
        }else{
            $user = M('user')->where('id='.$vo['uid'])->find();
            $vo['author'] = $user['nickname'];
        }
        $vo['pics'] = $pics;
        $vo['t'] = date('Y-m-d H:i:s',$vo['t']);
        if($vo['do_t']){
            $vo['do_t'] = date('Y-m-d H:i:s',$vo['do_t']);
        }
        if($vo['sh_t']){
            $vo['sh_t'] = date('Y-m-d H:i:s',$vo['sh_t']);
        }
        $this->res['data'] = $vo;
        $this->response($this->res,'json');
    }
    
    /**
     * 执行任务
     */
    public function doTask()
    {
        if(!IS_POST){
            $this->res['code'] = 101;
            $this->res['msg'] = '请使用post提交参数';
            $this->response($this->res,'json');
        }
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        $pics = $json->pics;
        $remark = $json->remark;
        $id = $json->id;
        
        if(!$id){
            $this->res['code'] = 102;
            $this->res['msg'] = '接单id为空';
            $this->response($this->res,'json');
        }
        
        if(empty($pics)){
            $this->res['code'] = 103;
            $this->res['msg'] = '请上传任务凭证';
            $this->response($this->res,'json');
        }
        
        $data = array(
            'pics'=>implode(',', $pics),
            'remark'=>$remark,
            'do_t'=>time(),
            'status'=>1
        );
        
        $res = M('user_task')->where('id='.$id)->data($data)->save();
        if($res){
            $this->res['msg'] = 'success';
            $this->response($this->res,'json');
        }else{
            $this->res['code'] = 500;
            $this->res['msg'] = '操作失败，请稍后再试！';
            $this->response($this->res,'json');
        }
    }
}